package com.qzdsoft.erpcloud.service.sys.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.sys.SysTableAuth;
import com.qzdsoft.erpcloud.mapper.sys.SysRoleTableFieldMapper;
import com.qzdsoft.erpcloud.mapper.sys.SysTableAuthMapper;
import com.qzdsoft.erpcloud.service.sys.SysTableAuthService;
import com.qzdsoft.erpcloud.vo.sys.SysTableAuthInfo;
import com.qzdsoft.erpcloud.vo.sys.SysTableAuthQueryParam;
import com.qzdsoft.utils.enums.CommonStatus2Enum;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.sys.SysRoleStatus;

/**
 * Created by Admin
 */
@Service
public class SysTableAuthServiceImpl implements SysTableAuthService {
	
    @Autowired
    private SysTableAuthMapper sysTableAuthMapper;
    
    @Autowired
    private SysRoleTableFieldMapper sysRoleTableFieldMapper;

    @Override
    public SysTableAuth selectById(Integer id) {
        return sysTableAuthMapper.selectByPrimaryKey(id);
    }
    
    
//    @Override
//    public Response saveOrUpd(Integer id, String name, String remark, Integer status) {
//        Response r = null;
//        SysTableAuth auth = new SysTableAuth();
//        auth.setId(id);
//        auth.setName(name);
//        auth.setRemark(remark);
//        auth.setStatus(status);
//        if(null != id){
//            r = Response.success("共变更:" + sysTableAuthMapper.updateByPrimaryKey(auth) + "条！");
//        } else {
//            auth.setCtime(new Date());
//            Integer iid = sysTableAuthMapper.insert(auth);
//            r = Response.success("共增加1条记录!");
//        }
//        return r;
//    }

    @Override
	public Response saveOrUpd(SysTableAuth info) {
    	SysTableAuth auth = new SysTableAuth();
		if(info.getId()!=null) {//修改
			auth = sysTableAuthMapper.selectByPrimaryKey(info.getId());
			auth.setName(info.getName());
			auth.setStatus(info.getStatus());
			auth.setRemark(info.getRemark());
			sysTableAuthMapper.updateByPrimaryKey(auth);
		}else{
			info.setCtime(new Date());
			sysTableAuthMapper.insert(info);
		}
		return new Response(ResultEnum.SUCCESS);
	}


	@Override
    public Response delete(Integer id) {
        SysTableAuth auth = sysTableAuthMapper.selectByPrimaryKey(id);
        auth.setStatus(Integer.parseInt(SysRoleStatus.DEL_CODE));
        sysTableAuthMapper.updateByPrimaryKey(auth);
        return new Response(ResultEnum.SUCCESS);
    }

    @Override
    public List<SysTableAuth> getAllTableAuthList() {
        return sysTableAuthMapper.selectAll();
    }


    @Override
    public LayTableResponse<SysTableAuthInfo> selectData(SysTableAuthQueryParam info) {
    	PageHelper.startPage(info.getPage(), info.getLimit());
    	List<SysTableAuthInfo> list = sysTableAuthMapper.selectData(info);
        return new LayTableResponse(list);
    }

    @Override
    public List<String> queryAuthrizedTableFields(Integer uid, String tableid)
    {
        Integer hasAllFunction = sysRoleTableFieldMapper.hasAllAuth(uid);
        if(hasAllFunction==1) {
            return sysRoleTableFieldMapper.queryTableFields(tableid);
        }else {
            return sysRoleTableFieldMapper.queryAuthrizedTableFields(uid,tableid);
        }
    }


	@Override
	public List<SysTableAuthInfo> selectAllAuper() {
		// TODO Auto-generated method stub
		return sysTableAuthMapper.selectAllSuper();
	}
    
    
}
